package com.example.flightsearch.ui

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.example.flightsearch.data.Airport
import com.example.flightsearch.data.AirportRepository
import com.example.flightsearch.data.Favorite
import com.example.flightsearch.data.FavoriteRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import javax.inject.Inject

@HiltViewModel
class FlightSearchViewModel @Inject constructor(
    private val airportRepository: AirportRepository,
    private val favoriteRepository: FavoriteRepository
) : ViewModel() {
    var uiState by mutableStateOf(FlightSearchUiState())
        private set

    suspend fun updateSearchText(query: String) {
        airportRepository.getAirportByCodeOrName(query).collect { airports ->
            uiState = uiState.copy(airports = airports)
        }
    }

    suspend fun search(code: String) {
        uiState = uiState.copy(flights = emptyList<Flight>().toMutableList())
        airportRepository.getAirportByCode(code)
            .filterNotNull()
            .collect { airport ->
                airportRepository.getAirportByNotCode(code).collect { airports ->
                    val flights = airports.map { destinationAirport ->
                        val isFavorite = favoriteRepository.have(
                            Favorite(
                                departureCode = airport.code,
                                destinationCode = destinationAirport.code
                            )
                        )
                        Flight(
                            departureCode = airport.code,
                            destinationCode = destinationAirport.code,
                            departureName = airport.name,
                            destinationName = destinationAirport.name,
                            isFavorite = isFavorite.first()
                        )
                    }
                    uiState = uiState.copy(flights = flights.toMutableList())
                }
            }
    }

    suspend fun toggleFavorite(flight: Flight) {
        val index = uiState.flights.indexOf(flight)
        if (index != -1) {
            uiState = uiState.copy(flights = uiState.flights.toMutableList().also {
                it[index] = flight.copy(isFavorite = !flight.isFavorite)
            })

            val favorite = Favorite(
                departureCode = flight.departureCode,
                destinationCode = flight.destinationCode
            )
            val hasFavorite = favoriteRepository.have(favorite)
            if (hasFavorite.first()) {
                favoriteRepository.delete(favorite)
            } else {
                favoriteRepository.add(favorite)
            }
        }
    }
}

data class FlightSearchUiState(
    val airports: List<Airport> = emptyList(),
    val flights: MutableList<Flight> = mutableListOf()
//    val isLoading: Boolean = false,
//    val error: String? = null
)

data class Flight(
    val departureCode: String,
    val destinationCode: String,
    val departureName: String,
    val destinationName: String,
    val isFavorite: Boolean = false
)